home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / ZARRAY.C < prev    next >
C/C++ Source or Header  |  1991-05-20  |  3KB  |  118 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zarray.c */
  21. /* Array operators for GhostScript */
  22. #include "memory_.h"
  23. #include "ghost.h"
  24. #include "alloc.h"
  25. #include "errors.h"
  26. #include "oper.h"
  27. #include "packed.h"
  28. #include "store.h"
  29.  
  30. /* The generic operators (copy, get, put, getinterval, putinterval, */
  31. /* length, and forall) are implemented in zgeneric.c. */
  32.  
  33. /* Forward references */
  34. private int make_array(P4(os_ptr, int, int, char *));
  35.  
  36. /* array */
  37. int
  38. zarray(register os_ptr op)
  39. {    int code = make_array(op, t_array, a_all, "array");
  40.     if ( code < 0 ) return code;
  41.     refset_null(op->value.refs, r_size(op));
  42.     return 0;
  43. }
  44.  
  45. /* aload */
  46. int
  47. zaload(register os_ptr op)
  48. {    ref aref;
  49.     int type;
  50.     aref = *op;
  51.     switch ( (type = r_type(&aref)) )
  52.        {
  53.     default: return e_typecheck;
  54.     case t_array: case t_mixedarray: case t_shortarray: ;
  55.        }
  56.     check_read(aref);
  57. #define asize r_size(&aref)
  58.     if ( asize > ostop - op ) return e_rangecheck;
  59.     if ( type == t_array )
  60.         memcpy((char *)op, (char *)aref.value.refs,
  61.                asize * sizeof(ref));
  62.     else
  63.        {    register ushort i;
  64.         ushort *packed = aref.value.packed;
  65.         os_ptr pdest = op;
  66.         for ( i = 0; i < asize; i++, pdest++ )
  67.             packed_get(packed, pdest),
  68.             packed = packed_next(packed);
  69.        }
  70.     push(asize);
  71. #undef asize
  72.     *op = aref;
  73.     return 0;
  74. }
  75.  
  76. /* astore */
  77. int
  78. zastore(register os_ptr op)
  79. {    uint size;
  80.     check_type(*op, t_array);
  81.     check_write(*op);
  82.     size = r_size(op);
  83.     if ( size > op - osbot ) return e_stackunderflow;
  84.     refcpy_to_old(op->value.refs, op - size, size, "astore");
  85.     op[-(int)size] = *op;
  86.     pop(size);
  87.     return 0;
  88. }
  89.  
  90. /* ------ Initialization procedure ------ */
  91.  
  92. op_def zarray_op_defs[] = {
  93.     {"1aload", zaload},
  94.     {"1array", zarray},
  95.     {"1astore", zastore},
  96.     op_def_end(0)
  97. };
  98.  
  99. /* ------ Internal procedures ------ */
  100.  
  101. /* Make an array from the operand stack. */
  102. /* Don't fill it in. */
  103. private int
  104. make_array(register os_ptr op, int type, int attrs, char *client_name)
  105. {    ref *abody;
  106.     uint size;
  107.     check_type(*op, t_integer);
  108.     if ( op->value.intval < 0 ||
  109.          op->value.intval > max_uint / sizeof(ref) - 1
  110.        )
  111.         return e_rangecheck;
  112.     size = op->value.intval;
  113.     abody = alloc_refs(size, client_name);
  114.     if ( abody == 0 ) return e_VMerror;
  115.     make_tasv(op, type, attrs, size, refs, abody);
  116.     return 0;
  117. }
  118.